package es.cazadoresmadridejos.beans.manager.advertisement.impl;

import org.displaytag.pagination.PaginatedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import es.cazadoresmadridejos.beans.dao.advertisement.AdvertisementDao;
import es.cazadoresmadridejos.beans.manager.advertisement.AdvertisementManager;
import es.cazadoresmadridejos.common.bean.dao.Dao;
import es.cazadoresmadridejos.common.bean.manager.impl.ManagerImpl;
import es.cazadoresmadridejos.model.Advertisement;
import es.cazadoresmadridejos.objs.criteria.AdvertisementCriteria;
import es.cazadoresmadridejos.objs.web.Page;
import es.cazadoresmadridejos.util.FileUtils;

@Service
public class AdvertisementManagerImpl extends ManagerImpl<Advertisement> 
implements AdvertisementManager{

	@Autowired 
	public void setDao(@Qualifier("advertisementDao")
	Dao<Advertisement> dao) {
		this.dao = dao;
	}

	@Override
	public PaginatedList findAllPaginated(Page page,
			AdvertisementCriteria advertisementCriteria) {
		return ((AdvertisementDao)dao).findAllPaginated(page,advertisementCriteria);
	}

	@Override
	public void delete(Long id) {

		Advertisement ad = get(id);
		if(ad.getImage() != null){
			FileUtils.deleteAdvertisement(ad.getImage());
		}
		super.delete(id);
	}

}
